package com.yaozheng.vocationaltraining.view.userinfo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.adapter.userinfo.ListViewSelectDialogAdapter;
import com.yaozheng.vocationaltraining.dialog.interfaces.BaseDialogEventProcess;
import com.yaozheng.vocationaltraining.utils.LogUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

/**
 * 列表选中对话框
 */
@EViewGroup(R.layout.view_dialog_listview_select)
public class ListViewSelectDialogView extends LinearLayout {
    @ViewById
    TextView baseDialogOperateExplanationId;
    @ViewById
    ListView baseDialogListView;


    ListViewSelectDialogAdapter listViewSelectDialogAdapter;

    private BaseActivity baseActivity;

    private ListViewSelectDialogViewItemClick listViewSelectDialogViewItemClick;

    public ListViewSelectDialogView(Context context) {
        super(context);
        this.baseActivity = (BaseActivity) context;
    }

    public ListViewSelectDialogView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.baseActivity = (BaseActivity) context;
    }

    public ListViewSelectDialogViewItemClick getListViewSelectDialogViewItemClick() {
        return listViewSelectDialogViewItemClick;
    }

    public void setListViewSelectDialogViewItemClick(ListViewSelectDialogViewItemClick listViewSelectDialogViewItemClick) {
        this.listViewSelectDialogViewItemClick = listViewSelectDialogViewItemClick;
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        baseDialogOperateExplanationId.setText(title);
    }

    public void setDataList(String[] dataStrs) {
        setDataList(stringArrayToJsonArray(dataStrs));
    }


    public void setDataList(JSONArray dataList) {
        if (listViewSelectDialogAdapter == null) {
            listViewSelectDialogAdapter = new ListViewSelectDialogAdapter(baseActivity, dataList);
            baseDialogListView.setAdapter(listViewSelectDialogAdapter);
        } else {
            if (dataList != listViewSelectDialogAdapter.getDataList()) {
                listViewSelectDialogAdapter.setDataList(dataList);
                listViewSelectDialogAdapter.notifyDataSetChanged();
            }
        }

    }

    /**
     * 一项点击
     */
    @ItemClick(R.id.baseDialogListView)
    public void itemClick(int position) {
        LogUtils.println("itemClick");
        if (listViewSelectDialogViewItemClick != null) {
            listViewSelectDialogViewItemClick.itemClick(position);
        }
    }

    public static JSONArray stringArrayToJsonArray(String[] dataStrs) {
        JSONArray dataList = new JSONArray();
        for (
                int i = 0;
                i < dataStrs.length; i++) {
            JSONObject itemJsonObject = new JSONObject();
            itemJsonObject.put("name", dataStrs[i]);
            dataList.add(itemJsonObject);
        }
        return dataList;
    }

    /**
     * 一项点击
     */
    public static interface ListViewSelectDialogViewItemClick {
        void itemClick(int position);
    }

}
